//package com.example.api.client;
//
//import cn.hutool.core.util.RandomUtil;
//import cn.hutool.http.HttpUtil;
//import cn.hutool.json.JSONUtil;
//import com.example.api.common.ErrorCode;
//import com.example.api.exception.BusinessException;
//import com.example.api.model.vo.UserKeyInfo;
//import com.example.api.service.UserService;
//import com.example.api.utils.RedisUtil;
//import com.example.api.utils.SignUtils;
//import org.apache.commons.lang3.StringUtils;
//import org.example.jingclientsdk.model.HealthInfo;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//
//import javax.servlet.http.HttpServletRequest;
//import java.util.HashMap;
//import java.util.Map;
//
//@Component
//public class CustomApiClient {
//
//    @Autowired
//    private UserService userService;
//
//    @Autowired
//    private RedisUtil redisUtil;
//
//    private String accessKey;
//
//    private String secretKey;
//
//    public CustomApiClient(String secretKey, String accessKey) {
//        this.secretKey = secretKey;
//        this.accessKey = accessKey;
//    }
//
//    private void validateAuth(HttpServletRequest request) {
//        String accessKey = request.getHeader("accessKey");
//        String nonce = request.getHeader("nonce");
//        String timestamp = request.getHeader("timestamp");
//        String sign = request.getHeader("sign");
//        String body = request.getHeader("body");
//
//        if (StringUtils.isAnyBlank(accessKey, nonce, timestamp, sign, body)) {
//            throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
//        }
//        // 验证 accessKey
//        UserKeyInfo keyInfo = userService.getKeyInfo(accessKey);
//        if (keyInfo == null) {
//            throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
//        }
//
//        // 验证 nonce 随机数
//        redisUtil.lGet()
//
//    }
//
//
//    private Map<String, String> buildHeaderMap(String body) {
//        Map<String, String> map = new HashMap<>();
//        map.put("accessKey", accessKey);
//        // 不能直接传递密钥，需要签名后传递
////        map.put("secretKey", secretKey);
//        map.put("nonce", RandomUtil.randomNumbers(4));
//        map.put("body", body);
//        map.put("timestamp", String.valueOf(System.currentTimeMillis() / 1000));
//        //  生成签名
//        map.put("sign", SignUtils.genSign(map, secretKey));
//        return map;
//    }
//
//
//    public String getHealth() {
//        String result = HttpUtil.get("http://106.55.106.201:8001/health");
//        boolean typeJSON = JSONUtil.isTypeJSON(result);
//        if (typeJSON) {
//            HealthInfo bean = JSONUtil.toBean(result, HealthInfo.class);
//            System.out.println(bean);
//        }
//        return result;
//    }
//
//
//    public static void main(String[] args) {
//        CustomApiClient customApiClient = new CustomApiClient();
//        String health = customApiClient.getHealth();
//        System.out.println(health);
//    }
//}
//
//
